<template>
  <el-cascader
    class="cascader-limited-scroll"
    v-model="salesGrade"
    placeholder="选择店铺分组"
    :options="cascadeList"
    :props="{ checkStrictly: true }"
    style="width: 300px; height: 30px"
    @change="changecascadeList"
  >
  </el-cascader>
</template>

<script>
import { listAll, importBiKeyword } from "@/api/chart/brandChannelShop";
export default {
  data() {
    return {
      salesGrade: [],
      cascadeList: [],
    };
  },
  created() {
    // this.getList();
    this.getdataSourceList();
  },
  methods: {
    changecascadeList() {
      this.$emit("shopDateArr", this.salesGrade);
    },
    /** 查询品牌渠道店铺关系列表 */
    getdataSourceList() {
      listAll().then((response) => {
        let list = [];
        response.forEach((item) => {
          if (item.isTalentLive == "是") {
            list.push(item);
          }
        });
        let result = this.transformData(list);
        this.cascadeList = result;
        this.salesGrade = [
          result[0].label,
          result[0].children[0].label,
          result[0].children[0].children[0].label,
        ];
        this.$emit("shopDateArr", this.salesGrade);
      });
    },

    //店铺渠道品牌转换

    transformData(data) {
      let result = [];

      data.forEach((item) => {
        let brand = item.brand;
        let channel = item.channel;
        let shopName = item.channelShopName;

        // 如果brand在结果数组中不存在，则创建一个新的数组
        let brandIndex = result.findIndex(
          (brandObj) => brandObj.label === brand
        );
        if (brandIndex === -1) {
          result.push({
            label: brand,
            value: brand,
            children: [],
            disabled: true,
          });
          brandIndex = result.length - 1;
        }

        // 如果channel在当前brand的children数组中不存在，则创建一个新的数组
        let channelIndex = result[brandIndex].children.findIndex(
          (channelObj) => channelObj.label === channel
        );
        if (channelIndex === -1) {
          result[brandIndex].children.push({
            label: channel,
            value: channel,
            children: [],
            disabled: true,
          });
          channelIndex = result[brandIndex].children.length - 1;
        }

        // 如果shopName在当前channel的children数组中不存在，则创建一个新的数组
        let shopIndex = result[brandIndex].children[
          channelIndex
        ].children.findIndex((shopObj) => shopObj.label === shopName);
        if (shopIndex === -1) {
          result[brandIndex].children[channelIndex].children.push({
            label: shopName,
            value: shopName,
            // children: []
          });
        }
      });

      return result;
    },
  },
};
